#!/bin/bash

# 检查是否提供了grep字符串参数
if [ $# -ne 1 ]; then
    echo "用法: $0 <grep字符串>"
    exit 1
fi

# 获取grep字符串参数
grep_string="$1"

# 设置输出文件名，以grep字符串为前缀，日期（精确到小时和分钟）为后缀
output_file="${grep_string}_$(date '+%Y%m%d_%H%M').txt"

# 初始化变量
sum=0
res_sum=0
declare -a sum_array
declare -a res_array
group_count=0

# 设置执行次数
repeat_times=30

# 清空输出文件
> "$output_file"

# 循环执行命令
for ((count=0; count<repeat_times; count++)); do
    # 获取当前时间戳
    timestamp=$(date '+%Y-%m-%d %H:%M:%S')
    echo "#$((count + 1)) $timestamp" >> "$output_file"
    echo "#$((count + 1)) $timestamp"

    # 执行命令并捕获输出
    command_output=$(adb shell COLUMNS=120 top -m 50 -n 1 | grep "$grep_string")

    # 将命令输出写入文件
    echo "$command_output" >> "$output_file"

    # 提取%CPU和RES并累加
    sum=0
    res_sum=0
    while IFS= read -r line; do
        cpu_value=$(echo "$line" | awk '{print $9}')
        res_value=$(echo "$line" | awk '{print $6}')
        
        if [[ $cpu_value =~ ^[0-9]+(\.[0-9]+)?$ ]]; then
            sum=$(echo "$sum + $cpu_value" | bc)
        fi
        if [[ $res_value =~ ^[0-9]+[Mm]$ ]]; then
            res_value=${res_value%[Mm]}
            res_sum=$(echo "$res_sum + $res_value" | bc)
        fi
    done <<< "$command_output"

    # 格式化当前组的%CPU和RES累加值
    formatted_sum=$(printf "%.1f%%" "$sum")
    formatted_res_sum=$(printf "%.1f MB" "$res_sum")
    
    echo "第 $((count + 1)) 组的%CPU累加值: $formatted_sum" >> "$output_file"
    echo "第 $((count + 1)) 组的RES内存用量: $formatted_res_sum" >> "$output_file"

    # 实时输出当前组的top数据和统计数据
    echo "$command_output"
    echo "第 $((count + 1)) 组的%CPU累加值: $formatted_sum"
    echo "第 $((count + 1)) 组的RES内存用量: $formatted_res_sum"

    # 保存当前组的总和
    sum_array+=("$sum")
    res_array+=("$res_sum")

    # 检查用户是否按了q键
    read -n 1 -s -t 1 input </dev/tty
    if [[ $input == "q" ]]; then
        break
    fi
done

# 计算%CPU和RES的均值、最低值和最高值
total_sum=0
total_res_sum=0
min_value=${sum_array[0]}
max_value=${sum_array[0]}
min_res_value=${res_array[0]}
max_res_value=${res_array[0]}

for value in "${sum_array[@]}"; do
    total_sum=$(echo "$total_sum + $value" | bc)
    if (( $(echo "$value < $min_value" | bc -l) )); then
        min_value=$value
    fi
    if (( $(echo "$value > $max_value" | bc -l) )); then
        max_value=$value
    fi
done

for res_value in "${res_array[@]}"; do
    total_res_sum=$(echo "$total_res_sum + $res_value" | bc)
    if (( $(echo "$res_value < $min_res_value" | bc -l) )); then
        min_res_value=$res_value
    fi
    if (( $(echo "$res_value > $max_res_value" | bc -l) )); then
        max_res_value=$res_value
    fi
done

# 计算均值
mean_value=$(echo "scale=1; $total_sum / ${#sum_array[@]}" | bc)
mean_res_value=$(echo "scale=1; $total_res_sum / ${#res_array[@]}" | bc)

# 格式化输出
formatted_min=$(printf "%.1f%%" "$min_value")
formatted_max=$(printf "%.1f%%" "$max_value")
formatted_mean=$(printf "%.1f%%" "$mean_value")
formatted_min_res=$(printf "%.1f MB" "$min_res_value")
formatted_max_res=$(printf "%.1f MB" "$max_res_value")
formatted_mean_res=$(printf "%.1f MB" "$mean_res_value")

# 将统计结果插入文件头部
{
    echo "%CPU均值: $formatted_mean"
    echo "%CPU最低值: $formatted_min"
    echo "%CPU最高值: $formatted_max"
    echo "RES均值: $formatted_mean_res"
    echo "RES最低值: $formatted_min_res"
    echo "RES最高值: $formatted_max_res"
    echo ""
} | cat - "$output_file" > temp && mv temp "$output_file"

# 实时输出总统计信息
echo "%CPU均值: $formatted_mean"
echo "%CPU最低值: $formatted_min"
echo "%CPU最高值: $formatted_max"
echo "RES均值: $formatted_mean_res"
echo "RES最低值: $formatted_min_res"
echo "RES最高值: $formatted_max_res"
